package com.siwei.mes.service.sync.tc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.config.CompanyConfigInterceptor;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.sync.tc.SupplyTask;
import com.siwei.mes.enums.SyncEnum;
import com.siwei.mes.enums.TcContrastExperimentTypeEnum;
import com.siwei.mes.mapper.syncTc.SupplyTaskMapper;
import com.siwei.mes.service.sync.tc.SupplyTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 砼材-供货任务(SupplyTask)表服务实现类
 *
 * @author linzi
 * @since 2024-05-22 21:08:27
 */
@Slf4j
@Service
public class SupplyTaskServiceImpl implements SupplyTaskService {
    @Resource
    private SupplyTaskMapper supplyTaskMapper;

    @Override
    public Long create(SupplyTask entity) {
        supplyTaskMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(SupplyTask entity) {
        supplyTaskMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        supplyTaskMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<SupplyTask> getPage(PageQuery<SupplyTask, SupplyTask> query) {
        SupplyTask searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> supplyTaskMapper.getList(searchParam));
    }

    @Override
    public SupplyTask loadById(Long id) {
        return supplyTaskMapper.loadById(id);
    }

    @Override
    public List<SupplyTask> getAll(SupplyTask entity) {
        return supplyTaskMapper.getList(entity);
    }

    @Override
    public Long insertSupplyTaskByExperiment(Experiment experiment) {
        try {
            long maxSupplyTaskId = supplyTaskMapper.getMaxSupplyTaskId();

            SupplyTask supplyTask = new SupplyTask();
            supplyTask.setSupplyTaskId(maxSupplyTaskId + 10001);
            supplyTask.setSupplyTaskCode("RW" + supplyTask.getSupplyTaskId());
            supplyTask.setProjectName(experiment.getMatterName());
            supplyTask.setProjectSpecs(experiment.getMatterSpecs());
            TcContrastExperimentTypeEnum typeEnum = TcContrastExperimentTypeEnum.selectByMesType(experiment.getExperimentType());
            supplyTask.setProjectCategory(typeEnum.getTcType());
            supplyTask.setFactory(experiment.getFactory());
            supplyTask.setSupplyPlanQuantity(BigDecimal.valueOf(experiment.getBehalfNumber()));
            supplyTask.setSupplySignQuantity(BigDecimal.valueOf(experiment.getBehalfNumber()));
            supplyTask.setTransportQuantity(BigDecimal.valueOf(experiment.getBehalfNumber()));
            supplyTask.setAbnormalQuantity(BigDecimal.ZERO);
            supplyTask.setSupplyCompanyCompany(experiment.getSupplyCompanyName());
            supplyTask.setSupplyCompanyAbbreviation(experiment.getSupplyCompanyCalled());
            supplyTask.setLogisticsCompanyCompany(null);
            supplyTask.setMixingPlantCompany(CompanyConfigInterceptor.getCompanyConfig(1).getCompanyName());
            supplyTask.setSupplyPlanTime(new Date());
            supplyTask.setTaskStartTime(new Date());
            supplyTask.setState(4);
            supplyTask.setSyncType(SyncEnum.PC.getCode());
            supplyTaskMapper.insertSelective(supplyTask);
            return supplyTask.getId();
        } catch (Exception e) {
            log.info("PC新增试验委托模拟创建原材料供货任务失败，错误信息：【{}】", e.getMessage(), e);
            assertTrue("创建原材料供货任务失败");
        }
        return null;
    }
}
